﻿using System;
using System.Diagnostics;
using FeedAgregator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using DDNY.WebPortals.Common;
using System.Xml;
using System.ServiceModel.Syndication;
namespace DDNYWebPortals.Common.Tests
{


  /// <summary>
  ///This is a test class for AgregatorTest and is intended
  ///to contain all AgregatorTest Unit Tests
  ///</summary>
  [TestClass()]
  public class AgregatorTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    ///A test for Aggregate
    ///</summary>
    [TestMethod()]
    public void AggregateTest()
    {
      //Agregator.StartAggregate();
      //  Assert.Inconclusive("A method that does not return a value cannot be verified.");
      Agregator.Aggregate();
    }

    /// <summary>
    ///A test for ProcessTagsForOldFeedItems
    ///</summary>
    [TestMethod()]
    public void ProcessTagsForOldFeedItemsTest()
    {

      Agregator.ProcessTagsForOldFeedItems();

    }

    /// <summary>
    ///A test for ProcessSeoUrlsForOldItems
    ///</summary>
    [TestMethod()]
    public void ProcessSeoUrlsForOldItemsTest()
    {
      Agregator.ProcessSeoUrlsForOldItems();

    }

    /// <summary>
    ///A test for ProcessFeed
    ///</summary>
    [TestMethod()]
    [DeploymentItem("FeedAgregator.dll")]
    public void ProcessFeedTest()
    {
      Feed feedItem = null; // TODO: Initialize to an appropriate value

      using (FeedSignalDataContext ctx = new FeedSignalDataContext())
      {
        feedItem = (from feeds in ctx.Feeds
                    where feeds.ID == 294
                    select feeds).Single();
        Agregator_Accessor.ProcessFeed(feedItem, ctx);
      }

      Assert.Inconclusive("A method that does not return a value cannot be verified.");
    }


    [TestMethod()]
    public void MigrateFromXmlToDB()
    {
      foreach (DDNY.WebPortals.Common.FeedCategory category in FeedXmlHelper.CurrentFeedCategories)
      {
        using (FeedSignalDataContext ctx = new FeedSignalDataContext("data source=.;initial catalog=forumsynd;user id=web;password=web"))
        {
          FeedAgregator.FeedCategory catToAdd = new FeedAgregator.FeedCategory();
          catToAdd.CategoryName = category.Title;
          catToAdd.UpdateIntervalInMinutes = 10;
          ctx.FeedCategories.InsertOnSubmit(catToAdd);
          ctx.SubmitChanges();
          foreach (DDNY.WebPortals.Common.RssFeed rssFeed in category.Feeds)
          {
            Feed feedToAdd = new Feed();
            feedToAdd.CategoryID = catToAdd.ID;
            feedToAdd.DisplayName = rssFeed.FeedTitle;
            feedToAdd.FeedName = rssFeed.ShortTitle;
            feedToAdd.FeedSource = UrlHelper.ExtractDomainNameFromURL(rssFeed.FeedUrl);
            feedToAdd.FeedUrl = rssFeed.FeedUrl;
            feedToAdd.UpdateIntervalInMinutes = 10;
            ctx.Feeds.InsertOnSubmit(feedToAdd);
            ctx.SubmitChanges();
          }
        }
      }
    }

    [TestMethod]
    public void TestAllFeeds()
    {
      using (FeedSignalDataContext ctx = new FeedSignalDataContext("data source=tfsw2008;initial catalog=systemfs;user id=web;password=web"))
      {
        var allFeeds = from Feeds in ctx.Feeds
                       select Feeds;
        foreach (Feed feed in allFeeds)
        {
          try
          {
            XmlReader reader = XmlReader.Create(feed.FeedUrl);
            SyndicationFeed syndicationFeed = SyndicationFeed.Load(reader);
            Debug.WriteLine(feed.FeedUrl + " success");
          }
          catch (Exception ex)
          {
            Debug.WriteLine(feed.FeedUrl + ex.Message);
          }
        }
      }
    }

    /// <summary>
    ///A test for GetContents
    ///</summary>
    [TestMethod()]
    [DeploymentItem("FeedAgregator.dll")]
    public void GetContentsTest()
    {
        SyndicationItem nodeItem = null; // TODO: Initialize to an appropriate value
        string expected = string.Empty; // TODO: Initialize to an appropriate value
        string actual;
        actual = Agregator_Accessor.GetContents(nodeItem);
        Assert.AreEqual(expected, actual);
        Assert.Inconclusive("Verify the correctness of this test method.");
    }

    /// <summary>
    ///A test for GetContentString
    ///</summary>
    [TestMethod()]
    [DeploymentItem("FeedAgregator.dll")]
    public void GetContentStringTest()
    {
        string content = "ala bala portokala ala bala portokala ala bala portokala ala bala portokala ala bala portokala ala bala portokala ala bala portokala ala bala portokala ala bala portokala "; // TODO: Initialize to an appropriate value
        string expected = string.Empty; // TODO: Initialize to an appropriate value
        string actual;
        actual = Agregator_Accessor.GetContentString(content);
        Assert.AreEqual(expected, actual);
        Assert.Inconclusive("Verify the correctness of this test method.");
    }
  }
}
